<template>
  <div class="table">
    <div class="header">
      <slot name="header">
        <div class="title">{{ title }}</div>
        <div class="handle">
          <slot name="handle"></slot>
        </div>
      </slot>
    </div>

    <el-table
      :data="tableData"
      @selection-change="handleSelectionChange"
      :border="isBorder"
      row-key="id"
      :default-expand-all="true"
    >
      <!-- 多选框 -->
      <el-table-column label="" type="selection" width="55" v-if="isCheckbox" />
      <el-table-column label="" type="index" width="55" v-if="isIndex" />

      <!-- 内容 -->
      <template v-for="item in listData" :key="item.prop">
        <el-table-column
          :label="item.label"
          show-overflow-tooltip
          :width="item.width"
          align="center"
        >
          <template #default="scope">
            <slot :name="item.prop" :row="scope.row">
              {{ scope.row[item.prop] }}
            </slot>
          </template>
        </el-table-column>
      </template>

      <!-- 操作 -->
      <el-table-column label="操作" v-if="isOpeartion">
        <template #default="scope">
          <slot name="operates" :index="scope.$index" :row="scope.row"></slot>
        </template>
      </el-table-column>
    </el-table>

    <div class="footer">
      <slot name="footer" v-if="isPagination">
        <el-pagination
          v-model:currentPage="page.currentPage"
          v-model:page-size="page.pageSize"
          :page-sizes="[1, 5, 10, 50, 100]"
          layout="sizes, prev, pager, next"
          :total="pagetionTotal"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </slot>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    tableData: {
      type: Array,
      required: true,
    },
    listData: {
      type: Array,
      required: true,
    },
    page: {
      type: Object,
      default: () => ({ currentPage: 1, pageSize: 10 }),
    },
    pagetionTotal: {
      type: Number,
      default: 0,
    },
    isOpeartion: {
      type: Boolean,
      default: true,
    },
    isCheckbox: {
      type: Boolean,
      default: false,
    },
    isIndex: {
      type: Boolean,
      default: false,
    },
    isPagination: {
      type: Boolean,
      default: true,
    },
    title: {
      type: String,
      default: "",
    },
    isBorder: {
      type: Boolean,
      default: true,
    },
  },
  emits: ["selectionChange", "update:page"],
  setup(props, { emit }) {
    const handleSelectionChange = (value) => {
      emit("selectionChange", value);
    };

    const handleSizeChange = (pageSize) => {
      emit("update:page", { ...props.page, pageSize });
    };

    const handleCurrentChange = (currentPage) => {
      emit("update:page", { ...props.page, currentPage });
    };

    return {
      handleSelectionChange,
      handleSizeChange,
      handleCurrentChange,
    };
  },
};
</script>

<style scoped lang="less">
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px;
  // background-color: #606266;
  border: 1px solid #ebeef5;
  border-bottom: none;
  border-top-left-radius: 10px;
  border-top-right-radius: 10px;
  .title {
    font-size: 18px;
    font-weight: 600;
  }
}
.footer {
  background-color: #fff;
  padding: 6px 0 6px 6px;
}
</style>
